package com.tao.lab.bitmaprecycle;

import android.graphics.Bitmap;

import com.tao.lab.utils.Util;

/**
 * @ProjectName: lab
 * @Package: com.tao.lab.bitmaprecycle
 * @ClassName: AttributeStrategy
 * @Description: java类作用描述
 * @Author: wangxf
 * @CreateDate: 2022/5/26 9:19
 * @UpdateUser: 更新者
 * @UpdateDate: 2022/5/26 9:19
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
class AttributeStrategy implements LruPoolStrategy {
    private final AttributeStrategy.KeyPool keyPool = new AttributeStrategy.KeyPool();
    private final GroupedLinkedMap<AttributeStrategy.Key, Bitmap> groupedMap = new GroupedLinkedMap();

    AttributeStrategy() {
    }

    public void put(Bitmap bitmap) {
        AttributeStrategy.Key key = this.keyPool.get(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        this.groupedMap.put(key, bitmap);
    }

    public Bitmap get(int width, int height, Bitmap.Config config) {
        AttributeStrategy.Key key = this.keyPool.get(width, height, config);
        return (Bitmap)this.groupedMap.get(key);
    }

    public Bitmap removeLast() {
        return (Bitmap)this.groupedMap.removeLast();
    }

    public String logBitmap(Bitmap bitmap) {
        return getBitmapString(bitmap);
    }

    public String logBitmap(int width, int height, Bitmap.Config config) {
        return getBitmapString(width, height, config);
    }

    public int getSize(Bitmap bitmap) {
        return Util.getBitmapByteSize(bitmap);
    }

    public String toString() {
        return "AttributeStrategy:\n  " + this.groupedMap;
    }

    private static String getBitmapString(Bitmap bitmap) {
        return getBitmapString(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    }

    private static String getBitmapString(int width, int height, Bitmap.Config config) {
        return "[" + width + "x" + height + "], " + config;
    }

    static class Key implements Poolable {
        private final AttributeStrategy.KeyPool pool;
        private int width;
        private int height;
        private Bitmap.Config config;

        public Key(AttributeStrategy.KeyPool pool) {
            this.pool = pool;
        }

        public void init(int width, int height, Bitmap.Config config) {
            this.width = width;
            this.height = height;
            this.config = config;
        }

        public boolean equals(Object o) {
            if (!(o instanceof AttributeStrategy.Key)) {
                return false;
            } else {
                AttributeStrategy.Key other = (AttributeStrategy.Key)o;
                return this.width == other.width && this.height == other.height && this.config == other.config;
            }
        }

        public int hashCode() {
            int result = this.width;
            result = 31 * result + this.height;
            result = 31 * result + (this.config != null ? this.config.hashCode() : 0);
            return result;
        }

        public String toString() {
            return AttributeStrategy.getBitmapString(this.width, this.height, this.config);
        }

        public void offer() {
            this.pool.offer(this);
        }
    }

    static class KeyPool extends BaseKeyPool<AttributeStrategy.Key> {
        KeyPool() {
        }

        public AttributeStrategy.Key get(int width, int height, Bitmap.Config config) {
            AttributeStrategy.Key result = (AttributeStrategy.Key)this.get();
            result.init(width, height, config);
            return result;
        }

        protected AttributeStrategy.Key create() {
            return new AttributeStrategy.Key(this);
        }
    }
}
